<?php
/**
 * 任务分销
 * 1.普通会员 一级分销 上级返佣百分比
 * */
function doTaskFx($receive_order)
{
    //判断此订单是否处理过分销
    if ($receive_order['fx_state']) {
        return true;
    }

    $task = pdo_get('yq_haircircle_task', array('id' => $receive_order['taskid']));
    if (empty($task) || $task['delete_time'] > 0) {
        return true;
    }

    //判断任务是否完成
    if ($receive_order['receive_status'] < 3) {
        return true;
    }

    $fx_log = '../attachment/fxtasklog/' . (date('Y-m-d_H:i:s') . '.txt');
    if (!file_exists($fx_log)) {
        mkdirs($fx_log);
    }

    // 分销配置
    $fenxiao_setting = cmf_get_option('fenxiao_setting_v1.0');
    if (!$fenxiao_setting['fenxiao_switch']) {
        return true;//没有开启分销
    }
    pdo_begin();
    //分销逻辑开始
    try {
        $child = pdo_get('yq_haircircle_users', array('id' => $receive_order['user_id']), array('id', 'pid', 'mobile', 'user_money','user_level'));

        if (empty($child)) {
            pdo_rollback();
            file_put_contents($fx_log, $receive_order['id'] . ':无法获取用户ID' . $receive_order['user_id'] . '的信息，分销中断' . PHP_EOL, FILE_APPEND);
            return false;
        }

        if (empty($child['pid'])) {
            pdo_rollback();
            file_put_contents($fx_log, $receive_order['id'] . ':用户ID' . $receive_order['user_id'] . '无上级，无需进行分销' . PHP_EOL, FILE_APPEND);
            return false;
        }

        // 拥有上级 且上级是会员 执行分销
        $upUser = pdo_get('yq_haircircle_users', array('id' => $child['pid']), array('id', 'pid', 'mobile', 'user_money','user_level'));
        if (!empty($upUser) && $upUser['user_level'] == 1 && $fenxiao_setting['vip_task_first_per'] > 0) {
            $fxyj = number_format($task['task_money'] * $fenxiao_setting['vip_task_first_per'] / 100, 2);
            $res = pdo_update('yq_haircircle_users', array('user_money' => $upUser['user_money'] + $fxyj), array('id' => $upUser['id']));
            if (empty($res)) {
                pdo_rollback();
                return false;
            }
            //写入余额变更日志
            $mlog['oid'] = $receive_order['id'];
            $mlog['user_id'] = $upUser['id'];
            $mlog['type'] = 2;
            $mlog['change'] = $fxyj;
            $mlog['mobile'] = $upUser['mobile'];
            $mlog['balance'] = $upUser['user_money'] + $fxyj;
            $mlog['remark'] = "直推下级({$child['id']})完成任务返佣:{$fxyj}";
            $mlog['create_time'] = time();
            $res = pdo_insert('yq_haircircle_user_money_log',$mlog);
            if(empty($res)){
                pdo_rollback();
                file_put_contents($fx_log, $receive_order['id'] . ':上级用户ID' . $upUser['id'] . '返佣失败，插入余额变更失败' . PHP_EOL, FILE_APPEND);
                return false;
            }
            //写入分销日志
            $sLog = [
                'oid' => $receive_order['id'],
                'yong_type' => 0,//任务佣金
                'user_id' => $upUser['id'],
                'sup_id' => $receive_order['user_id'],
                'fxprice' => $task['task_money'],
                'fxyj' => $fxyj,
                'remark' => "直推下级({$child['id']})完成任务返佣:{$fxyj}",
                'create_time' => time()
            ];

            $res = pdo_insert('yq_haircircle_user_fx_log',$sLog);
            if(empty($res)){
                pdo_rollback();
                file_put_contents($fx_log, $receive_order['id'] . ':上级用户ID' . $upUser['id'] . '返佣失败，写入分销日志失败' . PHP_EOL, FILE_APPEND);
                return false;
            }
        }
        $res = pdo_update('yq_haircircle_task_receive',array('fx_state'=>1),array('id'=>$receive_order['id']));
        if(empty($res)){
            pdo_rollback();
            file_put_contents($fx_log, $receive_order['id'] . ':上级用户ID' . $upUser['id'] . '返佣失败，更新fx_state状态失败' . PHP_EOL, FILE_APPEND);
            return false;
        }
        //更改分销状态fx_state
        pdo_commit();
    } catch (\Exception $e) {
        pdo_rollback();
        file_put_contents($fx_log, $receive_order['id'] . ':上级用户ID' . $upUser['id'] . '返佣失败，'.$e->getMessage() . PHP_EOL, FILE_APPEND);
        ajaxError('分销系统出错：' . $e->getMessage());
    }

    return true;
}